<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">

            <el-row type="flex" justify="end">
              <!-- 用一行布局 -->
              <!-- 在一行里面可以再分24列 -->
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- props的校验规则,必须与数组里面的数据名必须相同 -->
        <el-tree
          default-expand-all
          :data="list"
          :props=" {
            children: 'children',
            label: 'manager'
          }"
        >
          <template
            #default="{ data } "
          >
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px ;width: 100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">

                <el-row type="flex" justify="end">
                  <!-- 用一行布局 -->
                  <!-- 在一行里面可以再分24列 -->
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hadd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEid(data.id)">编辑部门</el-dropdown-item>

                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 子部门弹层 -->
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="isEdit?'编辑':'新增子部门'"
        :visible.sync="isshow"
        @close="hdialog"
      >
        <!-- 总是显示第一个,是因为创建了,没有被销毁 -->
        <!-- 利用 v-if 进行销毁弹层 -->
        <!-- 1. v-if="isshow" -->
        <!--  ref="myDialog" -->
        <!--   :is-edit="isEdit" -->
        <isShowDialog
          :id="curId"
          ref="hisShowDialog"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hsuccess"
          @close="isshow=false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartmentList, delDepartment } from '@/api/departments'

//
import { transDataToTreeData } from '@/utils/index'

//
import isShowDialog from './deptDialog.vue'
export default {
  components: { isShowDialog },
  data() {
    return {
      isshow: false,
      list: [],
      // defaultProps: {
      //   children: 'children',
      //   label: 'label'
      // }
      //
      curId: '',
      //
      isEdit: false,
      //
      originList: []
    }
  },
  created() {
    this.loadDepartmentList()
  },
  methods: {
    // 重置
    hdialog() {
      this.$refs.hisShowDialog.hreset()
    },
    // 删除部门
    hDel(id) {
      this.$confirm('您确定要删除吗').then(
        async() => {
          const res = await delDepartment(id)
          if (res.code !== 10000) return this.$message.error(res.message)
          this.$message.success(res.message)
          this.loadDepartmentList()
        }
      ).catch((error) => {
        console.log(error)
      })
    },
    // 编辑部门
    hEid(id) {
      this.curId = id
      this.isEdit = true // 修改
      this.isshow = true
      /* 利用refs 获取子组件里面的属性,或者方法 */
      /* 第二种办法 this.$nextTick 可以解决dom的异步更新问题, 原理是让数据更新在获取,在异步代码,后接着调用 nextTick */
      /* settimeout */
      // this.$nextTick(() => {
      //   this.$refs.myDialog.loadDepartment()
      // })
    },

    //
    hsuccess() {
      this.loadDepartmentList()
      this.isshow = false
    },
    //
    hadd(id) {
      // 显示弹层
      this.isshow = true
      // 更新当前操作的id
      this.curId = id
      this.isEdit = false // 新增
    },
    async loadDepartmentList() {
      try {
        const res = await getDepartmentList()
        console.log(res)

        this.originList = res.data.depts.map((item) => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
        // shift()  数组 的shift,得到删除的数组,原数组得到被删除后的数组
        // splice(),得到也是一个被删除的数组
        res.data.depts.shift()

        // 将平铺数据
        // transDataToTreeData(res.data.depts)
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }

}
</script>
